package com.fw.dao.impl;

import java.util.List;

import com.fw.dao.OrderDao;
import com.fw.model.Order;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class OrderDaoImpl implements OrderDao {

	@Override
	public boolean add_orderDao(Order order) {
		
		return order.save();
	}

	@Override
	public boolean update_orderByIdDao(Order order) {
		Record r = Db.findById("order", "id",order.getInt("id"))
				.set("ec_name", order.getStr("ec_name"))
				.set("ec_pwd", order.getStr("ec_pwd"))
				.set("customer_name", order.getStr("customer_name"))
				.set("customer_address", order.getStr("customer_address"))
				.set("goods_name", order.getStr("goods_name"))
				.set("RMB", order.getStr("RMB"))
				.set("amount", order.getInt("amount"))
				.set("goods_url", order.getStr("goods_url"))
				.set("note", order.getStr("note"))
				.set("submit_date", order.getDate("submit_date"))
				.set("static", order.getStr("static"))
				.set("u_id", order.getInt("u_id"));
		return Db.update("order","id", r);
	}

	@Override
	public boolean delete_orderByIdDao(Order order) {
		String sql = "delete from `order` where id = ?";
		int i = Db.update(sql, order.getInt("id"));
		if(i > 0) return true;
		return false;
	}

	@Override
	public Order query_orderByIdDao(Order order) {
		String sql = "select * from `order` where id = ?";
		List<Order> list = order.find(sql, order.getInt("id"));
		if(list.size() > 0) return list.get(0);
		return null;
	}

	@Override
	public Integer query_totalOrderByPageDao(String sql, List<Object> par) {
		String s = "select count(*) from `order` o where 1 = 1 "+ sql;
		long count = Db.queryLong(s, par.toArray());
		return (int) count;
	}

	@Override
	public List<Order> query_allOrderByPageDao(String sql, int firstPage,
			int maxPage, List<Object> par) {
		String s = "select * from `order` o where 1 = 1 "+sql +" limit ?,?";
		par.add(firstPage);
		par.add(maxPage);
		List<Order> list = Order.orderDao.find(s, par.toArray());
		return list;
	}

}
